<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="net.zjitc.eljstl.User" %>
<%@ page import="java.util.*" %><%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/6/11
  Time: 16:26
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>foreach</title>
</head>
<body>
<h2>使用foreac将数据展示在表格</h2>
<%
    User user=new User("tom",17,new Date());
    request.setAttribute("user",user);

    User user1=new User("tom",17,new Date());
    User user2=new User("lily",18,new Date());
    User user3=new User("张三",19,new Date());
    List<User> userList=new ArrayList<User>();
    userList.add(user1);
    userList.add(user2);
    userList.add(user3);
    request.setAttribute("userList",userList);


    /*Map<String,User> userMap=new HashMap<String, User>();
    userMap.put("tom",user1);
    userMap.put("lily",user2);
    userMap.put("zs",user3);
    request.setAttribute("userMap",userMap);
    使用Map需要加vlue/key值：
    <td>${u.vlue.name}</td>*/
%>
<h2>使用foreach将数据展示在表格</h2>
<table border="1" width="500" align="center">
<%--表头--%>
    <tr>
        <th>编号</th>
        <th>姓名</th>
        <th>年龄</th>
        <th>生日</th>
    </tr>
<%--数据行--%>
<%--items:用EL表示取出request/session/....域中的对象（集合）
    var：临时变量 ，含义：集合中的每一个元素（User对象）
    varStatus：循环状态，使用：count计数--%>
    <c:forEach items="${userList}" var="u" varStatus="s">
        <c:if test="${s.count%2==0}">
            <tr bgcolor="#8a2be2">
                <td>${s.count}</td>
                <td>${u.name}</td>
                <td>${u.age}</td>
                <td>${u.birStr}</td>
            </tr>
        </c:if>
        <c:if test="${s.count%2!=0}">
            <tr bgcolor="aqua">
                <td>${s.count}</td>
                <td>${u.name}</td>
                <td>${u.age}</td>
                <td>${u.birStr}</td>
            </tr>
        </c:if>

    </c:forEach>
</table>
</body>
</html>
